package cz.cernilovsky.android.profileapp.activities;

import java.io.IOException;

import org.json.JSONException;

import cz.cernilovsky.android.profileapp.R;
import cz.cernilovsky.android.profileapp.model.Profile;
import cz.cernilovsky.android.profileapp.services.ToastService;
import cz.cernilovsky.android.profileapp.storage.ProfilesStorage;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class ProfilesFragmentListActivity extends ListFragment {

	public static final String PROFILES = "profiles";
	private static final String TAG = "ProfilesFragmentListActivity";

/*	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);
		loadProfiles();
	}*/

	private void loadProfiles(Profile[] profiles) {
		if(profiles == null) {
			profiles = new Profile[0];
		}
		setListAdapter(new Adapter(profiles));
		/*getListView().setOnItemClickListener(new AdapterView.OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View v,
					int position, long id) {
				
			}
		});*/
		System.out.println("Loaded profiles in fragment");

	}
	
	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		//Log.d(TAG, "onItemClick");
		Profile p = ((ProfileView) v).getProfile();
		Intent i = new Intent(getActivity(), AddProfileActivity.class);
		i.putExtra(AddProfileActivity.PROFILE, p);
		startActivity(i);
	}

	void loadProfiles() {
		try {
			Profile[] profiles = ProfilesStorage.loadProfiles(getActivity());
			loadProfiles(profiles);
		} catch (IOException e) {
			ToastService.toastSettingsIOError(getActivity());
			e.printStackTrace();
		} catch (JSONException e) {
			ToastService.toastSettingsJSONError(getActivity());
			e.printStackTrace();
		}
	}
	
	class Adapter extends BaseAdapter {
		private Profile[] profiles;
		public Adapter(Profile[] profiles) {
			this.profiles = profiles;
		}

		@Override
		public int getCount() {
			return profiles.length;
		}

		@Override
		public Object getItem(int i) {
			return profiles[i];
		}

		@Override
		public long getItemId(int i) {
			return i;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Profile profile = (Profile) getItem(position);
			View view = null;
			if(convertView == null) {
				view = new ProfileView(getActivity(), profile);
			} else {
				((ProfileView) convertView).setProfile(profile);
				view = convertView;
			}
			return view;
		}
		
	}
	
	class ProfileView extends LinearLayout {
		private TextView profileName;
		private Button delete;
		private Profile profile;
		
		public ProfileView(Context context, Profile profile) {
			super(context);
			this.profile = profile;
			profileName = new TextView(context);
			profileName.setTextSize(20);
			LayoutParams params = new LayoutParams(0, LayoutParams.WRAP_CONTENT, 1);
			profileName.setLayoutParams(params);
			delete = new Button(context);
			delete.setText(R.string.delete);
			//setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
			setOrientation(LinearLayout.HORIZONTAL);
			setProfile(profile);
			addView(profileName);
			addView(delete);
			setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
		}

		public void setProfile(final Profile profile) {
			this.profile = profile;
			profileName.setText(profile.getName());
			delete.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					try {
						ProfilesStorage.deleteProfile(profile, ProfilesFragmentListActivity.this.getActivity());
						loadProfiles();
					} catch (IOException e) {
						ToastService.toastSettingsIOError(ProfilesFragmentListActivity.this.getActivity());
						e.printStackTrace();
					} catch (JSONException e) {
						ToastService.toastSettingsJSONError(getActivity());
						e.printStackTrace();
					}
				}
			});
		}
		
		public Profile getProfile() {
			return profile;
		}
		
	}

	@Override
	public void onStart() {
		super.onStart();
		loadProfiles();
	}

}
